import java.util.ArrayList;
import java.util.Scanner;
public class C_1581 {
    private static int min=99999;
    private static  ArrayList<Tep> b=new ArrayList<>();
    private static  ArrayList<String> stat=new ArrayList<>();
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int  m=input.nextInt();
        char a[]=input.next().toCharArray();
        Tep tep=new Tep();
        tep.step=0;
        tep.str=String.valueOf(a);
        stat.add(tep.str);
        b.add(tep);
        boolean fign=false;
        if(check(a))min=0;
        else
            while(b.size()!=0){
                Tep te=b.get(0);
                char[] strs=te.str.toCharArray();
                b.remove(0);
                for (int i = 0; i <m; i++) {
                    if(i+1<m){
                        char c=strs[i];
                        strs[i]=strs[i+1];
                        strs[i+1]=c;
                        Tep tes=new Tep();
                        tes.step=te.step+1;
                        if(check(strs)){
                            min=tes.step;
                            fign=true;
                            break;
                        }
                        tes.str=String.valueOf(strs);
                        if(!stat.contains(String.valueOf(strs))){
                            stat.add(String.valueOf(strs));
                            b.add(tes);
                        }
                        c=strs[i];
                        strs[i]=strs[i+1];
                        strs[i+1]=c;
                    }
                }
                if(fign)break;
            }
        if(min!=99999)
            System.out.println(min);
        else{
            System.out.println(-1);
        }
    }

    private static boolean check(char[] a) {
        String str=new String(a);
        if(str.contains("2012"))return true;
        return false;
    }
}

class Tep{
    public int step;
    public String str;

}